package com.rpc.center.register.client;

import com.rpc.common.entity.request.PackageType;
import com.rpc.common.entity.request.PublishDTO;
import com.rpc.common.entity.response.PublishRS;
import lombok.Setter;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

@Setter
public class SocketRegisterServiceImpl implements RPCRegisterService {
    private String address = "127.0.0.1";
    private int port = 9000;

    public SocketRegisterServiceImpl(String address, int port) {
        this.address = address;
        this.port = port;
    }

    @Override
    public boolean publish(PublishDTO publishDTO) {
        try {
            publishDTO.setPackageType(PackageType.PUBLISH_PACKAGE);
            Socket socket = new Socket(address, port);
            ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
            outputStream.writeObject(publishDTO);
            outputStream.flush();
            ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
            PublishRS publishRS = (PublishRS) inputStream.readObject();
            if (publishRS.isSuccess()) {
                System.out.println("注册服务成功");
            }
            outputStream.close();
            inputStream.close();
//            ObjectInputStream inputStream=new ObjectInputStream(socket.getInputStream());
//            inputStream.re
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return true;
    }
}
